//
//  PageViewController.m
//  Sky
//
//  Created by chen on 16/6/28.
//  Copyright (c) 2016年 chen. All rights reserved.
//

#import "PageViewController.h"
#import "DataViewController.h"
#import  "ModelController.h"

@interface PageViewController ()

@property (strong,nonatomic) UIPageViewController *pageViewController;
@property (strong,nonatomic) ModelController *modelController;

@end

@implementation PageViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"Page:%f",self.view.frame.size.height);
    
    // Do any additional setup after loading the view.
    self.pageViewController=[[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    self.modelController=[[ModelController alloc]init];
    DataViewController *startingViewController=[self.modelController viewControllerAtIndex:0 storyboard:self.storyboard];
    NSArray *viewControllers=@[startingViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    self.pageViewController.dataSource=self.modelController;
    
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    
    CGRect pageViewRect=self.view.bounds;
    self.pageViewController.view.frame=pageViewRect;
    [self.pageViewController didMoveToParentViewController:self];
    self.view.gestureRecognizers=self.pageViewController.gestureRecognizers;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshModelController) name:@"CityListDidChanged" object:nil];
}

-(void) refreshModelController{
    NSLog(@"refresh modelController");
    [self.modelController refresh];
    DataViewController *startingViewController=[self.modelController viewControllerAtIndex:(self.modelController.count-1) storyboard:self.storyboard];
    [self.pageViewController setViewControllers:@[startingViewController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}

@end
